package com.weilan.ark3.service;

import com.weilan.ark3.buffer.DataArray;
import com.weilan.ark3.buffer.DataArray2;
import com.weilan.ark3.common.GlobalParam;
import com.weilan.ark3.common.Result;
import com.weilan.ark3.common.ResultCodeEnum;
import com.weilan.ark3.entity.ControlDto;
import com.weilan.ark3.entity.FrontData;
import org.springframework.stereotype.Service;

@Service
/*
 * 前端数据
 * */
public class FrontDataService {

    public Result read(FrontData frontData) {
        Result<Object> result = new Result<>();
        if (frontData != null) {
            Integer slaveId = frontData.getSlaveId();
            Integer start = frontData.getStart();
            Integer value = frontData.getValue();
            short[] shorts = new short[value];
            if (start >= 0 && start < DataArray.data.length) {
                System.arraycopy(DataArray.data, start, shorts, 0, value);
                result.setData(shorts);
                result.setCode(ResultCodeEnum.SUCCESS.getCode());
                result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
            }

        }
        return result;
    }

    /*
     *防逆流，防超限
     * 时段功率time_power(峰谷)
     * 电网功率 grid_power(全局)  读取电网的功率
     * 设备数量 device_num(数据库)
     * 预设最大值 p_max(数据库)
     * 预设最容差 p_min(数据库)
     * */
    public  void preventReflux(int time_power, int grid_power, int device_num, int p_max, int p_min) {
        ControlDto controlDto = new ControlDto();
        //1.放逆流是否启动(标识)
        if (GlobalParam.nlState == 1) {
            if (DataArray.data != null && DataArray.data.length > 0 && DataArray2.data.length > 0 && DataArray2.data != null) {
                //获得系统状态
                //short sys_state = DataArray.data[100];
                //测试：
                short sys_state = 1;

                //获得是否为0功率保护
                //short state = DataArray.data[GlobalParam.onoff];

                //测试：
                short state =6;
                //放电
                /*short fangdian1 = DataArray.data[104];
                int fangdian = (int) (fangdian1 * 0.1);*/
                int fangdian =100;
                //充电
               /* short chongdian1 = DataArray.data[105];
                int chongdian = (int) (chongdian1 * 0.1);*/
                int chongdian =100;

                //判断是否存在通讯中断
                if (GlobalParam.commFail == 0) {
                    //判断当前系统状态（停止）
                    if (sys_state == 1) {
                        //停止时，判断系统的指令状态
                        //判断是否为0功率保护状态
                        if (state == 6) {
                            //判断放电
                            if (time_power > 0) {
                                int min = Math.min((GlobalParam.p_cmd + (grid_power - p_min)) / device_num, time_power);
                                int min1 = Math.min(min, fangdian);
                                controlDto.setSlaveId(GlobalParam.guangbo);
                                controlDto.setAddress(GlobalParam.power);
                                controlDto.setValue(min1);
                                //MsgQueue.writeToControlTcp(controlDto);
                                System.out.println("0功率保护，充电下发功率min1"+min1);
                                GlobalParam.p_cmd = min1;
                                //判断充电
                            } else if (time_power < 0) {
                                int max = Math.max((grid_power - (p_max - p_min) + GlobalParam.p_cmd), time_power);
                                int max1 = Math.max(max, chongdian);
                                controlDto.setSlaveId(GlobalParam.guangbo);
                                controlDto.setAddress(GlobalParam.power);
                                controlDto.setValue(max1);
                                //MsgQueue.writeToControlTcp(controlDto);
                                System.out.println("0功率保护，放电下发功率max1"+max1);

                                GlobalParam.p_cmd = max1;
                            }
                        } else if (state == 2) {
                            //启动系统
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.onoff);
                            controlDto.setValue(GlobalParam.start);
                            //MsgQueue.writeToControlTcp(controlDto);
                            System.out.println("启动系统");
                        }
                        //运行
                    } else if (sys_state == 2) {
                        //运行时
                        //时段功率  负载功率 电网功率 设备的数量  预设最大值 预设最小值
                        //判断放电
                        if (time_power > 0) {
                            int min = Math.min((GlobalParam.p_cmd + (grid_power - p_min)) / device_num, time_power);
                            int min1 = Math.min(min, fangdian);
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(min1);
                            //MsgQueue.writeToControlTcp(controlDto);
                            System.out.println("运行，放电下发功率min1"+min1);
                            GlobalParam.p_cmd = min1;
                        } else if (time_power < 0) {
                            int max = Math.max((grid_power - (p_max - p_min) + GlobalParam.p_cmd) / device_num, time_power);
                            int max1 = Math.max(max, chongdian);
                            controlDto.setSlaveId(GlobalParam.guangbo);
                            controlDto.setAddress(GlobalParam.power);
                            controlDto.setValue(max1);
                            //MsgQueue.writeToControlTcp(controlDto);
                            System.out.println("运行，充电下发功率max1"+max1);
                            GlobalParam.p_cmd = max1;

                        }
                        //故障
                    } else if (sys_state == 3) {
                        controlDto.setSlaveId(GlobalParam.guangbo);
                        controlDto.setAddress(GlobalParam.power);
                        controlDto.setValue(GlobalParam.zeroP);
                        //MsgQueue.writeToControlTcp(controlDto);
                        System.out.println("系统故障，下发0功率");

                    }
                } else {
                    //系统停机（下发0功率）
                    controlDto.setSlaveId(GlobalParam.guangbo);
                    controlDto.setAddress(GlobalParam.power);
                    controlDto.setValue(GlobalParam.zeroP);
                    //MsgQueue.writeToControlTcp(controlDto);
                    System.out.println("通讯中断，下发0功率");
                }

            }
        }

    }

}
